function _get(search) {
	var classElements = new Array();

	if(search.indexOf(".") == -1 && search.indexOf("/") == -1 )
	{
		return document.getElementById(search);
	}
	else if(search.indexOf("/") != -1)
	{
		var search = search.split("/");
		id = search[0];
		if(id == "" || id ==" ")
			id = document;
		tagName = search[1];
		id = _get(id);
		return id.getElementsByTagName(tagName);
	}
	else
	{
		var search = search.split(".");
		tag = search[0];
		if(tag == "" || tag==" ")
			tag="*";
		className = search[1];
		var elements = document.getElementsByTagName(tag);
		var elementsLength = elements.length;
		var classPattern = new RegExp("(^|\\\\s)"+className+"(\\\\s|$)");
		for (i = 0, j = 0; i < elementsLength; i++) 
		{
			if ( classPattern.test(elements[i].className) ) 
			{
				classElements[j] = elements[i];
				j++;
			}
		}
		return classElements;
	}
}

var Browser = 
{
	isIe : !navigator.appName.indexOf("Microsoft"),
	isFF : !navigator.appName.indexOf("Netscape"),
	isOpera : !navigator.appName.indexOf("Opera"),
	language : navigator.browserLanguage,
	version : parseInt(navigator.appVersion),
	cookiesEnabled : navigator.cookieEnabled,
	javaEnabled : navigator.javaEnabled(),
	platform : navigator.platform
}

var Ajax = {
	getXmlHttp: function(){
		if(Browser.isIe)
		{
			try{
				return new ActiveXObject("MSXML3.XMLHTTP");
			}
			catch(e){
				try{
					return new ActiveXObject("MSXML2.XMLHTTP");
				}
				catch(e){
					return new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
		}
		else 
		{
			try{
				return new XMLHttpRequest();
			}
			catch(e){
				alert("Your browser doesn't support Ajax.");
				return null;
			}
		}
	},
	Request: function(params){
		if (params) {
    		for (var name in params) {
      			this[name] = params[name];
    		}
  		}
  		var onComplete = this.onComplete;
		xmlHttp =  this.getXmlHttp();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==1)
			{
						  
			}
			if(xmlHttp.readyState==2)
			{
						  
			}
			if(xmlHttp.readyState==3)
			{
						  
			}
			if(xmlHttp.readyState==4)
			{
				
				if(this.responseType == "xml"){
					response = xmlHttp.resposneXml;
					onComplete(response);
				}
				else{
					response = xmlHttp.responseText;
					onComplete(response);
				}
					
			}
		}
		try
		{
			if(typeof this.method == "undefined")
				this.method = "POST";
			
			xmlHttp.open(this.method,this.url,true);
			if(this.method.toLowerCase() == "post"){
				params = (typeof this.parameters == "undefined" || this.parameters == null || this.parameters == "")?
						 null : this.parameters;
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Cache-Control", "no-cache");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close")
				xmlHttp.send(params);
			}
			else{
				xmlHttp.send(null);
			}
		}
		catch(e)
		{
			this.onFailure();
		}
	},
	update: function(elementName, url, method){
		xmlHttp =  this.getXmlHttp();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==1)
			{
						  
			}
			if(xmlHttp.readyState==2)
			{
						  
			}
			if(xmlHttp.readyState==3)
			{
						  
			}
			if(xmlHttp.readyState==4)
			{
				if(typeof elementName == "string")
					var element = _get(elementName);
				else
					var element = elementName;  
				
				
				element.innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.open(method,url,true);
		xmlHttp.send(null);
	}
} 
var Element = {
	hide: function(id){
		_get(id).display = 'none';
	},
	show: function(id){
		_get(id).display = '';
	}
}
var Form = {
	initialize: function(id){
		alert(id)
	},
	submit: function(options){
		if (options) {
    		for (var name in options) {
      			this[name] = options[name];
    		}
  		}
  		
		this.parameters = "";
		this.method = (_get(this.id).method == ""?"POST":_get(this.id).method);
		var inputFields = _get(this.id+"/input");
		var textAreas = _get(this.id+"/textarea");
		var dropdowns = _get(this.id+"/select");
		var radioButtons = [];
		var names = new Array();
		for(i=0; i<=inputFields.length-1; i++){
			if(i!=0 && (inputFields[i].type == "text" || inputFields[i].type == "checkbox" || inputFields[i].type == "password")){
				this.parameters += "&";
			}
			if(inputFields[i].type == "text"){
				if(inputFields[i].getAttribute('Required') == "true"){
					if(!Input.isDataEntered(inputFields[i].id)){
						alert(((typeof inputFields[i].name == "undefined") || (inputFields[i].name == "")? 
									inputFields[i].id: inputFields[i].name)+" is a required field.")
						return false;
					}
				}
				if(inputFields[i].getAttribute("SpecialCharacters") == "false"){
					if(!Input.isValid(inputFields[i].id)){
						alert(((typeof inputFields[i].name == "undefined") || (inputFields[i].name == "")? 
									inputFields[i].id: inputFields[i].name)+" is having some special characters.\n" +
											"Only alphabets are acceptable.")
						return false;
					}
				}
				if(inputFields[i].getAttribute("isPhoneNumber") == "true"){
					if(!Input.isValidPhoneNumber(inputFields[i].id)){
						alert(((typeof inputFields[i].name == "undefined") || (inputFields[i].name == "")? 
									inputFields[i].id: inputFields[i].name)+" is not a valid phone number.\n" +
											"Should contain ten digits.")
						return false;
					}
				}
				if(inputFields[i].getAttribute("isEmail") == "true"){
					if(!Input.isValidEmail(inputFields[i].id)){
						alert(((typeof inputFields[i].name == "undefined") || (inputFields[i].name == "")? 
									inputFields[i].id: inputFields[i].name)+" is not a valid e-mail address.\n" +
											"Example: someone@example.com")
						return false;
					}
				}
				if(inputFields[i].getAttribute("isIP") == "true"){
					if(!Input.isValidIP(inputFields[i].id)){
						alert(((typeof inputFields[i].name == "undefined") || (inputFields[i].name == "")? 
									inputFields[i].id: inputFields[i].name)+" is not a valid IP address.")
						return false;
					}
				}
				this.parameters += inputFields[i].id + "=" + inputFields[i].value; 
			}
			else if(inputFields[i].type == "checkbox"){
				this.parameters += inputFields[i].id + "=" + inputFields[i].checked;
			}
			else if(inputFields[i].type == "password"){
					if(!Input.isDataEntered(inputFields[i].id)){
						alert("Password field is a required field.")
						return false;
					}
				this.parameters += inputFields[i].id + "=" + inputFields[i].value;
			}
			else if(inputFields[i].type == "radio"){
				if(Browser.isIe){
					elementIndex = -1;
					for(j=0; j<names.length; j++){
						if(names[j] == inputFields[i])
							elementIndex = j;
					}
					if(elementIndex == -1){
						if(inputFields[i].checked){
							this.parameters += "&"+inputFields[i].name + "=" + inputFields[i].value;
							names[names.length] = inputFields[i].name;
						}
					}
				}
				else if(names.indexOf(inputFields[i].name) == -1){
						if(inputFields[i].checked){
							this.parameters += "&"+inputFields[i].name + "=" + inputFields[i].value;
							names.push(inputFields[i].name);
						}
				}
			}
		}
		
		for(i=0; i<=textAreas.length-1; i++){
			if(this.parameters.indexOf("&") != -1){
				this.parameters += "&";
			}
			if(inputFields[i].getAttribute('Required') == "true"){
				if(!Input.isDataEntered(inputFields[i].id)){
					return false;
				}
			}
			this.parameters += textAreas[i].id + "=" + textAreas[i].value; 
		}

		for(i=0; i<=dropdowns.length-1; i++){
			if(this.parameters.indexOf("&") != -1){
				this.parameters += "&";
			}
			this.parameters += dropdowns[i].id + "=" + dropdowns[i].value; 
		}
		
		var onComplete = this.onComplete;
		var parameters = this.parameters;
		var method = this.method;

		Ajax.Request({
			url: _get(this.id).action, 
			method: method,
			responseType: 'text', 
			parameters: parameters,
			onComplete: onComplete, 
			onFailure: function(){
				alert("Failed");
			} 
		});
		
		return false;
	},
	display: function(){
		
	}
} 


Dialog = function(){
	this.showDialog = function(options){
		id = Math.random();
		id = id * 100000;
		id = Math.floor(id);

	  	if(typeof options.url == "undefined" || options.url == "" || typeof options.url != "string"){
	  		alert("Proper URL is not specified to display a new dialog.");
	  		return;
	  	}
	  	if(typeof options.logoPath == "undefined" || options.logoPath == "" || typeof options.logoPath != "string"){
	  		var logoPath = "";
	  	}
	  	else{
	  		var logoPath = "<img src='"+options.logoPath+"' />";
	  	}
	  	if(typeof options.title == "undefined" || options.title == "" || typeof options.title != "string"){
	  		var title = " Title";
	  	}
	  	else{
	  		var title = options.title;
	  	}
	  	
	  	var dialog = document.createElement("div");
	  	dialog.style.zIndex = 1000;
	  	dialog.style.borderColor = 'lightgrey';
	  	dialog.style.width = '100px';
	  	dialog.style.height = '100px';
	  	dialog.id = id;
		dialog.style.border = '1px solid black';
	  	if(((typeof options.bgcolor != "undefined") || (options.bgcolor != "")) && (typeof options.bgcolor == "string"))
	  		dialog.style.backgroundColor = options.bgcolor;
	  		
	  	if(((typeof options.color != "undefined") || (options.color != "")) && (typeof options.color == "string"))
	  		dialog.style.color = options.color;
	  	
	  	if(typeof options.parentElement != "undefined"){
	  		parentElement = typeof options.parentElement == "string" ? _get(options.parentElement) : options.parentElement;
	  		dialog.style.width = parentElement.offsetWidth.toString()+"px";
	  		dialog.style.height =  parentElement.offsetHeight.toString()+"px";
	  		dialog.style.position = 'absolute';
	  		dialog.style.top = parentElement.offsetTop.toString()+"px";
	  		dialog.style.left = parentElement.offsetLeft.toString()+"px";
	  	}
	  	else{
	  		parentElement = false;
	  		if(!Browser.isIe)
	  			dialog.style.position = 'fixed';
	  		else
	  			dialog.style.position = 'absolute';
	  	}
		var dialogTitleBar = document.createElement("div");
	 	dialogTitleBar.style.top = '0px';
	 	dialogTitleBar.style.width = "100%";
	 	dialogTitleBar.id = "title_bar_"+this.id;

                dialogTitleBar.style.backgroundColor = options.titlebgcolor;
 	
	 	closeButton = document.createElement("span");
	 	closeButton.innerHTML = "<img src='/iGoogle/StyleSheets/close.png' />";
	 	closeButton.style.marginRight = "3px";
	 	closeButton.style.marginTop = "1px";
	  	
	  	closeButton.style.top = "5px";
	  	if(!Browser.isIe)
	  		closeButton.setAttribute('style','float:right;margin-right:3px;margin-top:1px');
	  	else
  			closeButton.style.styleFloat = 'right'
	  	
	  	closeButton.setAttribute('onclick',"Dialog.close("+id+")");
	  	
	  	dialogTitleBar.appendChild(closeButton);
	  	dialogTitleBar.innerHTML += logoPath + "<span font-family='verdana' font-size='10pt' style='margin-left:3px'>" + title + "</span>"; 
	  	dialog.appendChild(dialogTitleBar);
	  	
	  	dialogBody = document.createElement("div");
	  	dialogBody.id = "body_"+id;
	  	dialogBody.style.width = '100%';
	  	dialogBody.style.height = '100%';
	  	
	  	dialog.appendChild(dialogBody);
	  	
	  	Ajax.Request({
	  		method: "GET",
	  		url: options.url,
	  		onComplete: function(response){
				dialogBody.innerHTML = "<div id='response'>"+response+"</div>";	  	
                                alert(dialogBody.innerHTML);
	  		}
	  	});
	  	
	  	document.body.appendChild(dialog);
	  	if(!parentElement){
	  		dialog.style.top = ((document.body.clientHeight - dialog.offsetHeight)/2);
	  		dialog.style.left = ((document.body.clientWidth - dialog.offsetWidth)/2);
	  	}
	}
		
	this.showConfirm = function(options){

	}
		
	this.showError = function(options){

	}
		
	this.showWarning = function(options){

	}
}

	
Dialog.close = function(dialogId){
	dialogId = dialogId.toString();
	_get(dialogId).parentNode.removeChild(_get(dialogId));
}

ToolTip = function(){
	this.ajaxTip = function(options){
		
	}
	
	this.showToolTip = function(ev,html){
		var toolTip = document.createElement("div");
		toolTip.innerHTML = html;
		toolTip.style.MozOpacity = 0.6;
		toolTip.style.filter='alpha(opacity=60)';
		document.body.appendChild(toolTip);
		e = ev;
		alert(e.target)
		alert(e.srcElement.nodeName)
	}
} 

var Input = {
	isDataEntered: function(id){
		if(_get(id).value == "" || _get(id).value == " " || typeof _get(id).value == "undefined"){
			return false;
		}
		return true;
	},
	isValidPhoneNumber: function(id){
		phoneRegExp = new RegExp("[0-9]{10}");
		if(phoneRegExp.test(_get(id).value)){
			return true;
		}
		return false;
	},
	isValid: function(id){
		validRegExp = new RegExp("[A-Za-z]");
		if(validRegExp.test(_get(id).value)){
			return true;
		}
		return false;
	},
	isValidEmail: function(id){
		emailRegExp = new RegExp("[A-Za-z0-9._]+@[A-Za-z0-9.]+\.[A-Za-z0-9]+");
		if(emailRegExp.test(_get(id).value)){
			return true;
		}
		return false;
	},
	isValidIP: function(id){
		
	},
        isValidNumber: function(id){
              if(_get(id).value.isNaN())
                   return true;
        }
}


